//package com.loong.forehead.gateway.filter;
//
//import com.alibaba.fastjson.JSONObject;
//import com.loong.common.enumerate.ResultCode;
//import com.loong.common.rest.Result;
//import com.loong.forehead.gateway.common.RequestUtils;
//
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.util.PathMatcher;
//import org.springframework.util.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//
///**
// * 黑名单校验器
// *
// * 所有在黑名单内的请求全部拦截，拒绝访问
// */
//@Slf4j
//public class BlacklistFilter implements GlobalFilter, Ordered {
//
////    private BlacklistHandler blacklistHandler;
//
//    /**
//     * 排除签名验证的API
//     */
//    private List<String> signExclude;
//
//    /**
//     * 路径匹配验证器
//     */
//    private PathMatcher pathMatcher = new AntPathMatcher();
//
//    public BlacklistFilter(List<String> signExclude, BlacklistHandler blacklistHandler) {
//        this.signExclude = signExclude;
//        this.blacklistHandler = blacklistHandler;
//    }
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        // 如果是需要排除签名的路径，直接放行不验证
//        String requestPath = request.getURI().getRawPath();
//        if (checkRequestPath(requestPath)) {
//            return chain.filter(exchange);
//        }
//        ServerHttpResponse response = exchange.getResponse();
//        String deviceId = RequestUtils.getInstance().getDeviceId(request);
//        String ip = RequestUtils.getInstance().getIp(request);
//        // 设备号或IP为空直接返回限制访问
//        if (StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(ip)) {
//            log.debug("因为IP或设备号为空而限制访问");
//            return validateFail(response, ResultCode.ACCESS_ERROR, null);
//        }
//        // 检查设备号和IP是否能过检测
//        if (blacklistHandler.checkDeviceId(deviceId) || blacklistHandler.checkIp(ip)) {
//            log.debug("IP{}或设备号{}因为黑名单配置而限制访问", ip, deviceId);
//            return validateFail(response, ResultCode.ACCESS_ERROR, null);
//        }
//        return chain.filter(exchange);
//    }
//
//    @Override
//    public int getOrder() {
//        return Integer.MIN_VALUE;
//    }
//
//    private Mono<Void> validateFail(ServerHttpResponse response, ResultCode code, Object data) {
//        Result result = Result.fail(data,code.getMessage(),code);
//        String resultString = JSONObject.toJSONString(result);
//        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
//        DataBuffer buffer = response.bufferFactory().wrap(resultString.getBytes(StandardCharsets.UTF_8));
//        return response.writeWith(Mono.just(buffer));
//    }
//
//    private boolean checkRequestPath(final String requestPath) {
//        if (signExclude.contains(requestPath)) {
//            return true;
//        }
//        for (String excludePath : signExclude) {
//            if (pathMatcher.match(excludePath, requestPath)) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//}
